package ajaxtest;

import java.util.Random;

public class vder {
	public int[] getRandomFromArray(int[] array, int count) {
		// ArrayList<Integer>arrayList =null;
		int[] a = array;
		int[] result = new int[count];
		boolean r[] = new boolean[array.length];
		Random random = new Random();
		int m = count; // 要随机取的元素个数
		if (m > a.length || m < 0)
			return a;
		int n = 0;
		while (true) {
			int temp = random.nextInt(a.length);
			if (!r[temp]) {
				if (n == m) // 取到足量随机数后退出循环
					break;
				n++;
				System.out.println("得到的第" + n + "个随机数为：" + a[temp]);
				result[n - 1] = a[temp];
				r[temp] = true;
			}
		}
		return result;
	}

	public static void main(String[] args) {
		// Long along = System.currentTimeMillis();
		// int a[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9,
		// 10,11,12,13,14,15,16,17,18,19,20,21,22,23,24 };
		/*
		 * int a[] = new int[100]; for(int i =0;i<100;i++) a[i]=i; boolean r[] =
		 * new boolean[a.length]; Random random = new Random(); int m = 10; //
		 * 要随机取的元素个数 if (m > a.length || m < 0) return; int n = 0; Long along =
		 * System.currentTimeMillis(); while (true) { int temp =
		 * random.nextInt(a.length); if (!r[temp]) { if (n == m) // 取到足量随机数后退出循环
		 * break; n++; System.out.println("得到的第" + n + "个随机数为：" + a[temp]);
		 * r[temp] = true; } } Long bLong = System.currentTimeMillis();
		 * System.err.println(bLong-along); //System.err.println(a);
		 */
		vder vder = new vder();
		int a[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17,
				18, 19, 20, 21, 22, 23, 24 };
		System.out.println(vder.getRandomFromArray(a, 10));
	}
}
